package com.chance.cc.crawler.development.command.meta.font;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.downloader.*;
import com.chance.cc.crawler.development.command.HttpCommand;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * created by CC on 2020/11/26
 * mail 279020185@qq.com
 */
@Slf4j
public class CrawlerFontCommand extends HttpCommand {

    public CrawlerFontCommand(String host, int port){
        super(host,port);
    }

    public HttpPage parseTTFont(HttpRequest httpRequest,
                                HttpConfig httpConfig,
                                List<String> needParseList,
                                String domain){
        String url = commandUrl("parseTTFont");
        //compose crawler scheduler job
        //generate request
        HttpRequest request = new HttpRequest();
        request.setUrl(url);
        request.setMethod(HttpConstant.Method.POST);
        Map<String,Object> params = new HashMap<>();
        params.put("httpRequest",httpRequest);
        params.put("httpConfig",httpConfig);
        params.put("needParseList",needParseList);
        params.put("domain",domain);
        request.setRequestBody(HttpRequestBody.json(JSON.toJSONString(params),"utf-8"));
        HttpPage httpPage = doCommand(request);
        log.info("publish result :");
        log.info(httpPage.getRawText());
        return httpPage;
    }

    @Override
    public String initUrlPrefix(String host,int port) {
        return StringUtils.join("http://",host,":",port,"/crawler/font/api/v1");
    }
}
